package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.orgarea;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.common.utils.BootstrapUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.OrgAreaMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.OrgArea;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.OrgAreaVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.orgarea.JsmOrgAreaService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zmling on 2016/12/2.
 * 区域信息
 */

public class JsmOrgAreaServiceImpl implements JsmOrgAreaService {

	@Autowired
	private OrgAreaMapper orgAreaMapper;

	@Autowired
	private JsmEmployeeService employeeService;

	/**
	 * 查询
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrgArea> findOrgAreaOrgId(String orgId) throws Exception {
		return orgAreaMapper.findOrgAreaOrgId(orgId);
	}

	/**
	 * 新增
	 *
	 * @param orgArea
	 * @throws Exception
	 */
	@Override
	public void orgAreaAdd(OrgArea orgArea, Employee employee) throws Exception {
		orgArea.setAreaId(UniqueUtil.uuid());
		orgArea.setCreatorId(employee.getEmployeeId());
		Date date = new Date();
		orgArea.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
		orgArea.setOperateId(employee.getEmployeeId());
		orgArea.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
		orgArea.setStatus(0);
		orgAreaMapper.insertSelective(orgArea);
	}

	/**
	 * 查询列表
	 *
	 * @param bootstrapSearchBean
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONObject findOrgAreaByOrgId(BootstrapSearchBean bootstrapSearchBean) throws Exception {
		JSONObject jsonObject = new JSONObject();
		bootstrapSearchBean.setLimit(bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit());
		bootstrapSearchBean.setOffset(bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset());
		bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
		if (bootstrapSearchBean.getSearch() != null && bootstrapSearchBean.getSearch() != "") {
			bootstrapSearchBean.setSearch(bootstrapSearchBean.getSearch().trim());
		}
		jsonObject.put("total", orgAreaMapper.queryCount(bootstrapSearchBean));
		jsonObject.put("rows", JSON.toJSON(orgAreaMapper.queryList(bootstrapSearchBean)));
		return jsonObject;
	}

	/**
	 * 删除
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteOrgArea(Map orgId) throws Exception {
		int i = orgAreaMapper.updateOrgAreaByOrgId(orgId);
		return i;
	}

	/**
	 * 根据主键查询
	 *
	 * @param areaId
	 * @return
	 * @throws Exception
	 */
	@Override
	public OrgAreaVo loadOrgAreaView(String areaId) throws Exception {
		return orgAreaMapper.selectByPrimaryKey(areaId);
	}

	/**
	 * 修改
	 *
	 * @param orgArea
	 * @throws Exception
	 */
	@Override
	public void orgAreaUpdate(OrgArea orgArea, Employee employee) throws Exception {
		orgArea.setOperateId(employee.getEmployeeId());
		orgArea.setOperateDT(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
		orgAreaMapper.updateByPrimaryKeySelective(orgArea);
	}

	/**
	 * 区域查询返回List
	 *
	 * @param bootstrapSearchBean
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrgAreaVo> findOrgAreaVo(BootstrapSearchBean bootstrapSearchBean) throws Exception {
		return orgAreaMapper.findOrgAreaVo(bootstrapSearchBean);
	}
}
